//
//  SINATecScoreQueryDetailCourseViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/8.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecScoreQueryDetailCourseViewController.h"
#import "SINATecManagerQuerScoreCourseItemTableViewCell.h"
#import "SINATecScoreQueryDetailViewController.h"
#import "SINATecScoreQueryDetailListCourseViewController.h"
#import "SINATecCourseModel.h"
#import "SINAStuSchoolYearModel.h"
#import "SINACustomModelPickView.h"

@interface SINATecScoreQueryDetailCourseViewController ()<UITableViewDelegate,UITableViewDataSource,CustomModelPickViewDelegate>
//学年设置
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *noDataLabel;
//
@property(nonatomic,strong) NSMutableArray *dataList;
@property(nonatomic,strong) NSMutableArray *schoolYearDataList;
@property(nonatomic,strong) SINAStuSchoolYearModel *currentSchoolYearModel;
@end

@implementation SINATecScoreQueryDetailCourseViewController
-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return  _dataList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.schoolYearDataList = [[NSMutableArray alloc]init];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor=[UIColor colorWithHexString:@"#fafafa"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    for (NSInteger i=0; i<0; i++) {
        SINATecCourseModel *model = [[SINATecCourseModel alloc]init];
        model.courseTitle=@"中国古代史导论";
        [self.dataList addObject:model];
    }
    
    [self.tableView reloadData];
    
    [self loadingSchoolYearListNetData];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.titleLabel andWithBlick:^(UIView *view) {
        [self showSelectSchoolYearFunction];
    }];
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.noDataLabel andWithBlick:^(UIView *view) {
        [self loadingSchoolYearSubClassListNetData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SINATecManagerQuerScoreCourseItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINATecManagerQuerScoreCourseItemTableViewCell"];
    
    if (cell == nil) {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"SINATecManagerQuerScoreCourseItemTableViewCell" owner:nil options:nil] firstObject];
        
    }
    SINATecCourseModel *model = self.dataList[indexPath.row];
    cell.itemClickBlock = ^(SINATecCourseModel *itemModel) {
        SINATecScoreQueryDetailListCourseViewController *controller = [[SINATecScoreQueryDetailListCourseViewController alloc]initWithNibName:@"SINATecScoreQueryDetailListCourseViewController" bundle:nil];
        SINATecCourseModel *model=itemModel;
        controller.navigationItem.title=model.courseTitle;
        controller.pageType=self.pageType;
        controller.model = model;
        controller.subClassModel=self.model;
        controller.schoolYearModel = self.currentSchoolYearModel;
        [self.navigationController pushViewController:controller animated:YES];
    };
    cell.model=model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

//获取学年信息
-(void)loadingSchoolYearListNetData{

    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //用户的uuid String uuid
    
    [dict setObject:[[SINACommonUserHelper share]getUserModel].userUid forKey:@"uuid"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,stuGetSchoolYearListUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.titleLabel.text=@"暂无学年信息";
                self.noDataLabel.hidden=NO;
                self.tableView.hidden=YES;
            } else {
                //有学年信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    SINAStuSchoolYearModel *model = [[SINAStuSchoolYearModel alloc] initWithDict:dic];
                    SINACommonPopModel *popModel = [[SINACommonPopModel alloc]init];
                    popModel.model=model;
                    popModel.title=[NSString stringWithFormat:@"%@ %@",model.schoolyear,model.term];
                    if (i==0) {
                        self.titleLabel.text=[NSString stringWithFormat:@"%@ %@",model.schoolyear,model.term];
                        self.currentSchoolYearModel = model;
                    }
                    [self.schoolYearDataList addObject:popModel];
                }
                
                [self loadingSchoolYearSubClassListNetData];
                
            }
            
        }else{
            self.titleLabel.text=@"暂无学年信息";
            self.noDataLabel.hidden=NO;
            self.tableView.hidden=YES;
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        self.noDataLabel.hidden=NO;
        self.tableView.hidden=YES;
        self.titleLabel.text=@"暂无学年信息";
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}


-(void) showSelectSchoolYearFunction{
    
    
    if (self.schoolYearDataList.count==0) {
        [self loadingSchoolYearListNetData];
        return;
    }
    
    SINACustomModelPickView *pickerSingle = [[SINACustomModelPickView alloc]init];
    
    [pickerSingle setDataArray:self.schoolYearDataList];
    [pickerSingle setDefalutSelectRowStr:0];
    pickerSingle.titleLabel.text=@"选择学年";
    pickerSingle.tag=100;
    
    [pickerSingle setDelegate:self];
    
    [pickerSingle show];
    [self.view endEditing:YES];
}

-(void)customModelPickView:(SINACustomModelPickView *)customPickView selectedTitle:(SINACommonPopModel *)selectedModel{
    self.currentSchoolYearModel = (SINAStuSchoolYearModel*)selectedModel.model;;
    self.titleLabel.text = selectedModel.title;
    [self loadingSchoolYearSubClassListNetData];
}
-(void)dismissCustomPickView:(SINACustomModelPickView *)customPickView{
    
}

#pragma mark - BasePickViewDelegate method
- (void)dismissBasePickView:(SINABasePickView *)customPickView
{
    
}

//加载课程信息
-(void)loadingSchoolYearSubClassListNetData{
    
    
    if (self.currentSchoolYearModel==nil) {
        [YPXToastView showToastViewWithText:@"请选择学年信息" andDuration:2 andParentView:self.view];
        return;
    }
    
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //用户的uuid String uuid
 
    //班级id String classifyid
    [dict setObject:self.model.classifyid forKey:@"classifyid"];
    //学年 String schoolyear
    [dict setObject:self.currentSchoolYearModel.schoolyear forKey:@"schoolyear"];
    //学期 String term
    [dict setObject:self.currentSchoolYearModel.term forKey:@"term"];
    [self.dataList removeAllObjects];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getSubjectByClassifyUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.noDataLabel.hidden=NO;
                self.tableView.hidden=YES;
            } else {
                //有班级信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    
                    SINATecCourseModel *model = [[SINATecCourseModel alloc]initWithDict:dic];
                    [self.dataList addObject:model];
                    
                }
                self.noDataLabel.hidden=YES;
                self.tableView.hidden=NO;
                
            }
            
            
        }else{
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            self.noDataLabel.hidden=NO;
            self.tableView.hidden=YES;
            
        }
        
        
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        [self.tableView reloadData];
        self.noDataLabel.hidden=NO;
        self.tableView.hidden=YES;
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}
@end
